import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { JwtResponseDto } from './dto/jwt-response.dto';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { AdminRegisterDto } from './dto/admin-register.dto';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  @UsePipes(new ValidationPipe())
  async login(@Body() loginDto: LoginDto): Promise<JwtResponseDto> {
    const user = await this.authService.validateUser(loginDto);
    return this.authService.login(user);
  }

  @Post('register')
  @UsePipes(new ValidationPipe())
  async register(@Body() createUserDto: CreateUserDto): Promise<JwtResponseDto> {
    return this.authService.register(createUserDto);
  }

  @Post('register/admin')
  @UsePipes(new ValidationPipe())
  async registerAdmin(@Body() adminRegisterDto: AdminRegisterDto): Promise<JwtResponseDto> {
    return this.authService.registerAdmin(adminRegisterDto.userData, adminRegisterDto.adminCode);
  }
}